/*
 Jeffrey Klarfeld (je542304)
 William Greenburg (wi328885)
 
 Assignment #1
 
 */

/********Function Prototypes********/
void stackPrint(); 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


/********Constant Values********/
#define MAX_STACK_HEIGHT 2000
#define MAX_CODE_LENGTH 500 
#define MAX_LEXI_LEVELS 3

/********Initial Values********/
//Note: Global variables may be a dirty way of doing this
int SP = 0;
int BP = 1;
int PC = 0;
int IR = 0;
int theStack[MAX_STACK_HEIGHT];

int toJump =0;


/********Suggested Struct Def********/
typedef struct instructions
{
    int op;
    int l; // L
    int m; // M
}instruction;



int main(int argc, const char * argv[])
{
    //Keep const char * argv[] as Eurip suggests that adding flags
    //would be a good idea, since we'll need it later
    
    //theStack[0] = 0;
    theStack[1] = 0;
    theStack[2] = 0;
    theStack[3] = 0;

    int i = 0;
    int ProgramCounter = 0;

    //Instruction Register
    instruction instructionRegister[MAX_STACK_HEIGHT]; //For now, change later? 
    //Size may not matter unless he dictated this in class, but I can't remember
        
    FILE *input;
    //Don't know whether we're writing to file or screen.
    //FILE *output;

    // Let's check whether or not we need to get this from 
    // Runtime arguements or not.
    char* inputFile = "input.txt";    

    input = fopen(inputFile, "r");
    
    if (inputFile == NULL)
    {
        printf("File could not be found\n");
        
        return 0;
    }
    
    while(!feof(input))
    {
        fscanf(input, "%d %d %d", &instructionRegister[ProgramCounter].op, &instructionRegister[ProgramCounter].l, &instructionRegister[ProgramCounter].m);
        ProgramCounter++;
    }

    //PRINT DAT INPUT YO!

    printf("Line     OP     L     M\n");

    for(i=0;i<ProgramCounter-1;i++)
    {
        if(i < 10)
        {
            printf("0%d ", i);    
        }
        else
        {
            printf("%d ", i);
        }
        
        switch(instructionRegister[i].op)
        {
            case 1:
                printf("      lit");
                break;
            case 2:
                printf("      opr");
                break;
            case 3:
                printf("      lod");
                break;
            case 4:
                printf("      sto");
                break;
            case 5:
                printf("      cal");
                break;
            case 6:
                printf("      inc");
                break;
            case 7:
                printf("      jmp");
                break;
            case 8:
                printf("      jpc");
                break;
            case 9:
                printf("      sio");
                break;
            case 10:
                printf("      sio");
                break;
            default: 
                printf("      ERR");
                break;
        }

        printf(" %4d %5d\n", instructionRegister[i].l, instructionRegister[i].m);   
    }

    int instructionCount = ProgramCounter;

    ProgramCounter = 0;

    

    return 0;
}

